<?php
// Init owncloud

require_once(JPATH_SITE.'/components/com_city/helper.php');

OCP\JSON::callCheck();
if(!OC_User::isLoggedIn()) {
   echo '您需要先登录';
   exit;
} else {
   if (PFactory::isJobseekerUser()) {
      echo "您无权访问该资源！";
      exit;
   }
}

$location_ids = JRequest::getString('ids', 0);
$ids = empty($location_ids) ? array() : explode(',', $location_ids);

$subcity = JRequest::getString('subcity', 0);
$province_id =  JRequest::getInt('pid',0);
$location_cids =  JRequest::getString('cids',0);
$cids = empty($location_cids) ? array() : explode(',', $location_cids);

$state =JRequest::getString('state',null);
$id_state = JRequest::getInt('id_state',0);
$id_city = JRequest::getInt('id_city',0);
$source = JRequest::getString('source',null);
$source_id_city = JRequest::getInt('source_id_city',0);

if($source=='zhilian' && !empty($id_state) && empty($id_city)){
   $all_city_list = ProvinceHelper::getCities($id_state);
}else{
   if($subcity == 'subcity'){
      $provincelist = ProvinceHelper::getProvinceFullList(1,'',$province_id);
   }else {
      $provincelist = ProvinceHelper::getProvinceFullList(1,'10000,10001,10002,10003');
      $hotarealist = ProvinceHelper::getHotCityList();
      $selectcityinfo = empty($location_ids) ? array() : ProvinceHelper::getCityInfoByCityIds($location_ids);
   } 
}
?>
<?php if($source=='zhilian' && !empty($id_state) && empty($id_city)):?>
   
   <div class="dialogp_content">
      <div class="selectcitytitle medium"><b><?php echo $state;?>所有城市：</b></div>
      <div class="postioncitylisttable medium allcitylist" style="height:auto; border-top:0px; margin-top:0px;">
         <table width="100%" border="0" cellpadding="0" cellspacing="0">
         <?php 
            $city_list = array_chunk($all_city_list,8);
            foreach ($city_list as $list) {
               echo '<tr>';
               foreach ($list as $h){
                  echo '<td width="11%">';
                  if($h['cityid']==$source_id_city){
                     echo '<a href="javascript:;" class="selectcity cur_a" id="city_'.$h['cityid'].'" data-id_city="'.$h['cityid'].'" data-city="'.$h['city'].'">'.$h['city'].'</a>';
                  }else{
                     echo '<a href="javascript:;" class="selectcity" id="city_'.$h['cityid'].'" data-id_city="'.$h['cityid'].'" data-city="'.$h['city'].'">'.$h['city'].'</a>';
                  }
                  echo '</td>';
               }
               echo '</tr>';
            }
         ?>
         </table>
      </div>
   </div>
   <script type="text/javascript">
      $(".dialogp_content .allcitylist").on("click","a.selectcity",function(){
         var target = $(this);
         if(target.hasClass('cur_a')){
            target.removeClass('cur_a');
         }else{
            target.addClass('cur_a');
            target.parent().siblings().children('a.cur_a').removeClass('cur_a');
            target.parent().parent().siblings().find('a.cur_a').removeClass('cur_a');
         }
      })
   </script>
<?php else:?>
      <?php if($subcity != 'subcity'): ?>
      <div class="dialogp_content">
         <div class="subacct_postarget">
            <ul id="areatagsbox" class="tagbox inputbox"></ul>
         </div>
         
         <div class="selectcitytitle medium"><b>热门工作地点：</b></div>
         <div class="postioncitylisttable medium hotcitylist" style="height:auto; border-top:0px; margin-top:0px;">
            <table width="100%" border="0" cellpadding="0" cellspacing="0">
            <?php 
               $hotarealist = array_chunk($hotarealist,8);
               foreach ($hotarealist as $hotarea) {
                  echo '<tr>';
                  foreach ($hotarea as $h){
                     echo '<td width="11%">';
                     if($h['provinceid'] == '10000' || $h['provinceid'] == '10001' || $h['provinceid'] == '10002' || $h['provinceid'] == '10003') {
                        echo '<a href="javascript:;" rel="'. $h['provinceid'] .'"  class="selectarea" name="area_'. $h['provinceid'] .'">'.$h['name'].'</a>';
                     }else {
                        echo '<a href="javascript:;" class="selecthotcity" id="hotcity_'.$h['cityid'].'" data="{\'cid\':\''.$h['cityid'].'\',\'pid\':\''.$h['provinceid'].'\',\'text\':\''.addslashes($h['province']).'-'.addslashes($h['name']).'\'}" >'.$h['name'].'</a>';
                     }
                     echo '</td>';
                  }
                  echo '</tr>';
               }
            ?>
            </table>
         </div>
         
         <div class="selectcitytitle medium"><b>其他工作地点：</b></div>
         <div class="postioncitylisttable medium" id="other_job_cities" style="height:auto; border-top:0px; margin-top:0px;">
            <table width="100%" border="0" cellpadding="0" cellspacing="0">
            <?php 
 
               $provincedisplaylist = array_chunk($provincelist,6);
               foreach ($provincedisplaylist as $province) {
                  echo '<tr>';
                  foreach ($province as $p){
                     echo '<td width="17%"><a href="javascript:;" class="selectarea" name="area_'. $p['provinceid'] .'" rel="'. $p['provinceid'] .'" >'.$p['province'].'</a></td>';
                  }
                  echo '</tr>';
               }
            ?>
            </table>
         </div>
      </div>
   
   <script type="text/javascript">
   $("#areatagsbox").tagit({
      maxNumTags: 0,
      itemName: "area",
      fieldName: "ids",  
      allowUserInput: false,
      onTagRemoved: function(e, tag) {
          var jobid = tag.find('input').val();
          $("#hotcity_"+ jobid).removeClass('cur_a');
          $("input[name='city_"+ jobid+"']").attr('checked',false);
          var tagclass = tag.attr('class').split(' ');
          $("a[name=area_" + tagclass[1] + "]").removeClass('cur_a');
      }
   });

   $(".selectcity_dialog").on("click","a.selectarea",function(){
      var target = $(this);
      var cid = '';
      $("#areatagsbox li.tagit-choice").each(function() {
          var cityid = $(this).find("input").val();
          cid = (cid == '') ? cityid.toString() : cid + ',' + cityid.toString();
      });
      $.ajax({
          type: "GET",
          url: OC.filePath('jobposition', 'ajax', 'getpositionarea.php'),
          data: {ids:$('#location_ids').val(),subcity:'subcity',pid:target.attr('rel'),cids:cid},
          dataType: 'html',
          success: function(result) {
              $(".selectcity_listdialog").html(result).show().position({
                  my:"left top+10",
                  at:"left-10 bottom",
                  of:target,
                  collision:"none"
               });
          }
       });
   });

   $('#selectcity_dialog').on('hide', function (){
      $(".selectcity_listdialog").hide();
   });
   
   $('.postioncitylisttable').delegate('.selecthotcity', 'click', function(){
      var target = $(this);
      $.metadata.setType("attr", "data");
      if(target.hasClass('cur_a')){
         target.removeClass('cur_a');
         $("#areatagsbox").tagit('removeTagByName', target.metadata().cid);
      }else{
         target.addClass('cur_a');
         $("#areatagsbox").tagit("createTag", target.metadata().cid, target.metadata().pid, target.metadata().text);
      }
      $('.selectcity_listdialog').hide();
   });
   
   $('.selectcity_listdialog').delegate('.selectcity', 'change', function(){
      var target = $(this);
      $.metadata.setType("attr", "data");
      if(target.attr('checked')){
         if(target.metadata().pid == '10000' || target.metadata().pid == '10001' || target.metadata().pid == '10002' || target.metadata().pid == '10003') {
            target.parents('.selectcity_listdialog').find('input').each(function(){
               var data = $(this).metadata();
               if($(this).val() != target.val() && $(this).attr('checked')) {
                  $(this).attr('checked',false);
                  $("input[name='city_"+ $(this).val()+"']").attr('checked',false);
                  $("#areatagsbox").tagit('removeTagByName', $(this).val());
               }
            });
            $("a[name=area_" + target.metadata().pid + "]").addClass('cur_a');
         }
         $("#areatagsbox").tagit("createTag", target.val(), target.metadata().pid , target.metadata().text);
         $("#hotcity_"+ target.metadata().cid).addClass('cur_a');
      }else{
         $("#areatagsbox").tagit('removeTagByName', target.metadata().cid);
         $("#hotcity_"+ target.metadata().cid).removeClass('cur_a');
         if(target.metadata().pid == '10000' || target.metadata().pid == '10001' || target.metadata().pid == '10002' || target.metadata().pid == '10003') {
            $("a[name=area_" + target.metadata().pid + "]").removeClass('cur_a');
         }
      }
   });

   <?php 
      foreach ($selectcityinfo as $city) {
         echo '$("#areatagsbox").tagit("createTag", "'.$city['cityid'].'", "'.$city['provinceid'].'", "'.($city['province'] = $city['city'] ? $city['province'] : $city['province'].'-'.$city['city']).'");';
         echo '$("#hotcity_'.$city['cityid'].'").addClass("cur_a");';
         echo '$(".hotcitylist a[name=area_'.$city['provinceid'].']").addClass("cur_a");';
      }
   ?>
   </script>
   
   <?php elseif ($subcity == 'subcity') :?>
      <?php
         $p = $provincelist[0];
         $muniCityId = ProvinceHelper::getMuniCityId($p['provinceid']);
         $input = '';
         if ($muniCityId) {
            $is_checked = in_array($muniCityId, $cids) ? 'checked' : '';
            $input = '<label class="checkbox inline"><input type="checkbox" name="city_'.$muniCityId.'"  '.$is_checked.'  class="selectcity" data="{\'cid\':\''.$muniCityId.'\',\'pid\':\''.$p['provinceid'].'\',\'text\':\''.addslashes($p['province']).'\'}" value="'.$muniCityId.'"></label>';
         }
         echo '<i></i>
               <i class="close_ibtn">&times;</i>
               <table width="100%" border="0" cellpadding="0" cellspacing="0">
                <tr>
                   <td width="20%">
                   <label class="inline" style="padding:5px 0 0 0">
                      <b>'.$input.$p['province'].'</b>
                   </label>
                   </td>
                   <td width="20%"></td>
                   <td width="20%"></td>
                   <td width="20%"></td>
                   <td width="20%"></td>
                </tr>';
              $cityidlist = explode(',',$p['cityid']);
              $citylist = explode(',',$p['city']);
              $cityidlist = array_chunk($cityidlist,5,true);
              foreach ($cityidlist as $city) {
                  echo '<tr>';
                  foreach ($city as $key => $value) {
                      $ifchecked = in_array($value, $cids) ? 'checked' : '';
                      echo '<td width="20%">
                                 <label class="checkbox inline">
                                   <input type="checkbox" name="city_'.$value.'"  '.$ifchecked.'  class="selectcity" data="{\'cid\':\''.$value.'\',\'pid\':\''.$p['provinceid'].'\',\'text\':\''.addslashes($p['province']).'-'.$citylist[$key].'\'}" value="'.$value.'"> '.$citylist[$key].'
                                 </label>
                               </td>';
                  }
                  echo '</tr>';
              }
              echo '</table>';
      ?>
   <?php endif; ?>
<?php endif;?>
<div><input type="hidden" name="dialog_source" id="dialog_source" value="<?php echo $source;?>"/></div>